﻿using System;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using EmptyWallGallery.Core;

namespace EmptyWallGallery.FrontEnd.inside.Manage
{
    public partial class Register : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            var roleArray = Roles.GetAllRoles();
            cboList.DataSource = roleArray;
            cboList.DataBind();

            Page.Title = "Thêm mới thành viên - " + Configs.WebTitle;
        }

        public void UpdateRolesFromList(string username, CheckBoxList checkBoxList)
        {
            foreach (ListItem roleListItem in checkBoxList.Items)
            {
                var roleName = roleListItem.Value;
                var userName = username;
                var enableRole = roleListItem.Selected;

                if (enableRole & (Roles.IsUserInRole(userName, roleName) == false))
                {
                    Roles.AddUserToRole(userName, roleName);
                }
                else if ((enableRole == false) & Roles.IsUserInRole(userName, roleName))
                {
                    Roles.RemoveUserFromRole(userName, roleName);
                }
            }
        }

        protected void BtnCreateUserClick(object sender, EventArgs e)
        {
            switch (RegisterUser())
            {
                case MembershipCreateStatus.Success:
                    var user = Membership.GetUser(UserName.Text);
                    if (user == null) return;

                    UpdateRolesFromList(user.UserName, cboList);

                    var strPasswordTemp = user.ResetPassword();
                    Core.Utilities.Common.Instance.SendEmailPassword(Configs.WebName + " - Thong tin dang nhap",
                                                                     user.UserName, user.Email, strPasswordTemp);

                    Response.Redirect("EditUser.aspx?u=" + user.UserName + "&Msg=1&Pwd=" + strPasswordTemp);
                    break;
                case MembershipCreateStatus.DuplicateEmail:
                    lblMessage.Visible = true;
                    lblMessage.Text = "* Email đã tồn tại.";
                    break;
                case MembershipCreateStatus.DuplicateUserName:
                    lblMessage.Visible = true;
                    lblMessage.Text = "* Tên đăng nhập đã tồn tại.";
                    break;
                default:
                    lblMessage.Visible = true;
                    lblMessage.Text = "* Đã có lỗi trong quá trình đăng ký xin vui lòng liên hệ Administrator để biết thêm chi tiết.";
                    break;
            }
        }

        public MembershipCreateStatus RegisterUser()
        {
            MembershipCreateStatus membershipCreateStatus;
            
            Membership.CreateUser(UserName.Text, "1234567", Email.Text,
                                                       "Bạn thích nhất con gì?", "Mèo", true, out membershipCreateStatus);

            return membershipCreateStatus;
        }
    }
}